package net.interneteidee.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


import javax.ejb.Asynchronous;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import net.interneteidee.entity.Fattura;
import net.interneteidee.entity.Ordine;
import net.interneteidee.servizi.OrdineFatturaService;
import net.interneteidee.utility.ComponentName;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;


@ManagedBean
@SessionScoped
@Asynchronous
public class StampaController implements ComponentName {

	private JRBeanCollectionDataSource beanCollectionDataSource;
	private JasperPrint jasperPrint;
	
	private String pathAssolutoReportOrdine,pathAssolutoSubReportOrdine;
	private String pathAssolutoReportFattura,pathAssolutoSubReportFattura;
	private String pathAssolutoLogo;
	
	@Inject
	OrdineFatturaService ordineFatturaService;
	
	public StampaController() {
		 //TODO Auto-generated constructor stub
		pathAssolutoReportOrdine = FacesContext.getCurrentInstance().getExternalContext().getRealPath(pathReportOrdine)+File.separator;
		pathAssolutoSubReportOrdine = FacesContext.getCurrentInstance().getExternalContext().getRealPath(pathSubReportOrdine)+File.separator;
		
		pathAssolutoReportFattura = FacesContext.getCurrentInstance().getExternalContext().getRealPath(pathReportFattura)+File.separator;
		pathAssolutoSubReportFattura = FacesContext.getCurrentInstance().getExternalContext().getRealPath(pathSubReportFattura)+File.separator;
		
		pathAssolutoLogo = FacesContext.getCurrentInstance().getExternalContext().getRealPath(pathLogo);
	}
	
	

	

	public void stampaOrdine(Ordine ordine) throws JRException, IOException{
		
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("LOGO",pathAssolutoLogo);
		param.put("SUBREPORT_DIR", pathAssolutoSubReportOrdine);
		
		List<Ordine> ordini = new LinkedList<Ordine>();
		ordini.add(ordine);
		
		beanCollectionDataSource = new JRBeanCollectionDataSource(ordini);
		
		jasperPrint = JasperFillManager.fillReport(pathAssolutoReportOrdine,param, beanCollectionDataSource);
		HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
	   // httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");  
	    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();  
	    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  
	    FacesContext.getCurrentInstance().responseComplete();
		
	}
	
	public void stampaOrdini(List<Ordine> ordini) throws JRException, IOException{
		
		Map<String,Object> param = new HashMap<String, Object>();
		
		param.put("LOGO",pathAssolutoLogo);
		param.put("SUBREPORT_DIR", pathAssolutoSubReportOrdine);
		beanCollectionDataSource = new JRBeanCollectionDataSource(ordini);
		
		jasperPrint = JasperFillManager.fillReport(pathAssolutoReportOrdine,param, beanCollectionDataSource);
		HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
	   // httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");  
	    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();  
	    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  
	    FacesContext.getCurrentInstance().responseComplete();
		
	}
		
	public void stampaFattura(Fattura fattura) throws JRException, IOException{
			
			Map<String,Object> param = new HashMap<String, Object>();
			param.put("LOGO",pathAssolutoLogo);
			param.put("SUBREPORT_DIR",pathAssolutoSubReportFattura);
			
			
			List<Fattura> fatture = new LinkedList<Fattura>();
			fatture.add(fattura);
			
			beanCollectionDataSource = new JRBeanCollectionDataSource(fatture);
			
			jasperPrint = JasperFillManager.fillReport(pathAssolutoReportFattura,param, beanCollectionDataSource);
			HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
		  //  httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");  
		    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();  
		    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  
		    FacesContext.getCurrentInstance().responseComplete();
			
		}
		
	public void stampaFatture(List<Fattura> fatture) throws JRException, IOException{
			
			Map<String,Object> param = new HashMap<String, Object>();
		
			param.put("LOGO",pathAssolutoLogo);
			param.put("SUBREPORT_DIR", pathAssolutoSubReportFattura);
			beanCollectionDataSource = new JRBeanCollectionDataSource(fatture);
			
			jasperPrint = JasperFillManager.fillReport(pathAssolutoReportFattura,param, beanCollectionDataSource);
			HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
		//    httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");  
		    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();  
		    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  
		    FacesContext.getCurrentInstance().responseComplete();
			
		}	
	
	@GET
	@Path("fattureinscadenza")
	public void stampaFattureInScadenzaWS() throws JRException, IOException{
		
		Map<String,Object> param = new HashMap<String, Object>();
		
		param.put("LOGO",pathAssolutoLogo);
		param.put("SUBREPORT_DIR", pathAssolutoSubReportFattura);
		
		List<Fattura> fattureInScadenza = ordineFatturaService.findFattureInScadenza();
		beanCollectionDataSource = new JRBeanCollectionDataSource(fattureInScadenza);
		
		jasperPrint = JasperFillManager.fillReport(pathAssolutoReportFattura,param, beanCollectionDataSource);
		HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
	//    httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");  
	    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();  
	    JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  
	    FacesContext.getCurrentInstance().responseComplete();
		
		
	}
	
	
}
